import { createRouter, createWebHashHistory } from 'vue-router';

const routes = [
  {
    path: '/login',
    name: 'LogIn',
    meta: { title: '登录' },
    component: () => import('@/views/LogIn.vue')
  },
  {
    path: '/',
    component: () => import('@/views/shared/Workbench.vue'),
    children: [
      {
        path: "/",
        meta: {
          title: "工作台",
          activeMenu: 0
        },
        name: "Index",
        component: () => import("@/views/Home.vue")
      },
      {
        path: '/about',
        name: 'About',
        meta: { title: '关于' },
        component: () => import('@/views/shared/About.vue')
      }
    ]
  },
  {
    path: '/:catchAll(.*)*',
    name: 'notfound',
    component: () => import('@/views/shared/Workbench.vue'),
    children: [{
      path: '',
      name: '404',
      component: () => import('@/views/shared/404.vue')
    }]
  }
];

const router = createRouter({
  history: createWebHashHistory('/'),
  routes
});

let app = null;
let proxy = new Proxy(router.install, {
  apply: function (target, ctx, args) {
    app = args[0];

    Reflect.apply(...arguments);
  }
});

router.install = proxy;

router.beforeEach((to, from, next) => {
  next();
});

router.afterEach((to, from) => {
  document.title = `${window.settings.title} | ${to.meta.title || ''}`;
});

let refreshToken = async () => {
  let store = $app._context.provides.store;
  let res = await $app.$http.get(`/AuthCenter/api/oauth/token/refresh/${store.state.oauth.access_token}`);

  return res;
};

export default router;